package cn.edu.bjfu.practice.practice.app.common.response;

import java.util.Objects;

public class Response<T> {
    /** 状态代码 */
    private String code;

    /** 提示信息 */
    private String message;

    /** 响应的数据 */
    private T data;

    /** 默认的状态码 */
    public static final String DEFAULT_SUCCESS_CODE = "00000";

    /** 默认的提示信息 */
    public static final String DEFAULT_SUCCESS_MESSAGE = "操作成功";

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) { return true; }
        if (o == null || getClass() != o.getClass()) { return false; }
        Response<T> response = (Response<T>) o;
        return Objects.equals(code, response.code) &&
                Objects.equals(message, response.message) &&
                Objects.equals(data, response.data);
    }

    @Override
    public int hashCode() {
        return Objects.hash(code, message, data);
    }

    /**
     * 不带参数的成功响应对象
     * @return 响应对象
     */
    public static <U> Response<U> success(){
        Response<U> response = new Response<U>();
        response.setCode(DEFAULT_SUCCESS_CODE);
        return response;
    }

    /**
     * 带数据的成功响应对象
     * @param data 自定义的消息
     * @return 响应对象
     */
    public static <U> Response<U> success(U data){
        Response<U> response = new Response<>();
        response.setCode(DEFAULT_SUCCESS_CODE);
        response.setData(data);
        return response;
    }


    /**
     * 带消息的成功响应对象
     * @return 响应对象
     */
    public static <U> Response<U> message(){
        Response<U> response = new Response<>();
        response.setCode(DEFAULT_SUCCESS_CODE);
        response.setMessage(DEFAULT_SUCCESS_MESSAGE);
        return response;
    }


    /**
     * 带消息的成功响应对象
     * @param message 自定义消息
     * @return 响应对象
     */
    public static <U> Response<U> message(String message){
        Response<U> response = new Response<>();
        response.setCode(DEFAULT_SUCCESS_CODE);
        response.setMessage(message);
        return response;
    }

    /**
     * 带消息的成功响应对象
     * @param message 自定义消息
     * @param data 自定义数据
     * @return 响应对象
     */
    public static <U> Response<U> message(String message, U data){
        Response<U> response = new Response<>();
        response.setCode(DEFAULT_SUCCESS_CODE);
        response.setMessage(message);
        response.setData(data);
        return response;
    }


    /**
     * 生成错误的响应对象
     * @param errorCode 错误代码
     * @return 响应对象
     */
    public static <U> Response<U> error(ErrorCode errorCode){
        Response<U> response = new Response<>();
        response.setCode(errorCode.toString());
        response.setMessage(errorCode.getMessage());
        return response;
    }

    /**
     * 生成错误的响应对象
     * @param errorCode 错误代码
     * @param message 自定义消息
     * @return 响应对象
     */
    public static <U> Response<U> error(ErrorCode errorCode, String message){
        Response<U> response = new Response<>();
        response.setCode(errorCode.toString());
        response.setMessage(message);
        return response;
    }
}
